package com.ashley.apptools.utils;

import com.ashley.apptools.provider.ToolsData.ItemNumber;
import com.ashley.apptools.vo.ItemVo;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;

public class ItemNoContentResolver {

	public static final String CATEGORY = ItemNumber.COLUME_CATEGORY+" = ?";
	public static final String ITEM_SORT_ORDER = ItemNumber.COLUME_ITEM_NO+" DESC";
	public static final String TIME_SORT_ORDER ="creationtime DESC";
	public static final String[] SMALL_DATA= new String[]{ItemNumber._ID,ItemNumber.COLUME_ITEM_NO};
	public static final String[] MEDIUM_DATA  = new String[]{ItemNumber._ID,ItemNumber.COLUME_ITEM_NO,ItemNumber.COLUME_FREIGHT,ItemNumber.COLUME_TAX,ItemNumber.CLUME_RETAIL};
	public static Cursor getItemNo(ContentResolver contentResolver,String[] projection,String sortOrder)
	{	
		Cursor cursor = contentResolver.query(ItemNumber.CONTENT_URI, projection, null,null, sortOrder);
		return cursor;		
	}
	public static Cursor getCategoryItem(ContentResolver contentResolver, String[] projection, int category,String sortOrder)
	{
		String[] selectionArgs = new String[]{String.valueOf(category)};
		String selection = CATEGORY;
		Cursor cursor = contentResolver.query(ItemNumber.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
		return cursor;
	};
	public static Cursor getItemID(ContentResolver contentResolver, int uid,String[] projection)
	{
		Uri uri = ContentUris.withAppendedId(ItemNumber.CONTENT_ID_URI_PATTERN, uid);
		Cursor cursor = null;
		try
		{
			cursor = contentResolver.query(uri, projection, null, null, null);
		}catch(IllegalArgumentException argumentException)
		{
			
		}catch(SQLException exception )
		{
			
		}
		return cursor;
		
	}
	
	public static ItemVo cursorToItemVo(Cursor cursor,int mode)
	{
		ItemVo vo =  new ItemVo();
		int id = cursor.getColumnIndex(ItemNumber._ID);
		int itemNo = cursor.getColumnIndex(ItemNumber.COLUME_ITEM_NO);
		vo.setId(cursor.getInt(id));
		vo.setItemNo(cursor.getString(itemNo));
			
		if(mode >0)
		{
			int freight = cursor.getColumnIndex(ItemNumber.COLUME_FREIGHT);
			int tax = cursor.getColumnIndex(ItemNumber.COLUME_TAX);
			int landing = cursor.getColumnIndex(ItemNumber.COLUME_LANDING);
			int warehovse = cursor.getColumnIndex(ItemNumber.COLUME_WAREHOVSE);
			int sale = cursor.getColumnIndex(ItemNumber.COLUME_SALE);
			int reital = cursor.getColumnIndex(ItemNumber.CLUME_RETAIL);
				
			vo.setFreight(cursor.getDouble(freight));
			vo.setTax(cursor.getDouble(tax));
			vo.setLanding(cursor.getDouble(landing));
			vo.setWarehouse(cursor.getDouble(warehovse));
			vo.setSale(cursor.getDouble(sale));
			vo.setRetail(cursor.getDouble(reital));
				
		}
		
		return vo;
		
	}
}
